package com.epalmpay.service.index.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;

import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.entity.*;
import com.epalmpay.entity.profit.Profit;

import com.epalmpay.mapper.*;
import org.springframework.stereotype.Service;

import com.epalmpay.dto.agent.AgentIndexInDTO;
import com.epalmpay.dto.agent.AgentIndexOutDTO;
import com.epalmpay.dto.agent.AgentStatisAgentDaySevenDataOutDTO;
import com.epalmpay.dto.mall.MallGoodOutDTO;
import com.epalmpay.dto.merchant.index.GroupActivityBaseOutDTO;
import com.epalmpay.dto.merchant.index.GroupIndexInDTO;
import com.epalmpay.dto.merchant.index.StatisGroupDayOutDTO;
import com.epalmpay.dto.shopindex.MemberCountOfflineOutDTO;
import com.epalmpay.dto.shopindex.ShopIndexInDTO;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.service.index.IIndexService;

@Service("indexService")
public class IndexServiceImpl implements IIndexService{

	@Resource
	private StatisAgentDayMapper statisAgentDayMapper;
	@Resource
	private StatisAgentMapper statisAgentMapper;
	
	@Resource
	private StatisAgentMonthMapper statisAgentMonthMapper;
	@Resource
	private DeviceMapper deviceMapper;
	@Resource
	private StatisShopMapper statisShopMapper;
	@Resource
	private MallGoodMapper mallGoodMapper;
	
	@Resource
	private StatisGroupDayMapper statisGroupDayMapper;
	
	@Resource
	private StatisGroupMapper statisGroupMapper;
	
	@Resource
	private ActivityBaseMapper activityBaseMapper;
	@Resource
	private TradeOfflineMapper tradeOfflineMapper;
	@Resource
	private OrderServiceGoodDetailMapper orderServiceGoodDetailMapper;
	@Resource
	private MemberMapper memberMapper;
	@Resource
	private ShopMapper shopMapper;
	@Resource
	private CouponUserMapper couponUserMapper;
	@Resource
	private StatisShopDayMapper statisShopDayMapper;
	@Resource
	private OrderServiceGoodMapper orderServiceGoodMapper;
	
	//@Resource
	//private ProfitAgentMapper profitAgentMapper;
	@Resource
	private DevicePosMapper devicePosMapper;

	@Resource
	private ProfitMapper profitMapper;

	@Resource
	private AgentMapper agentMapper;
	@Resource
	private OrderGoodItemMapper orderGoodItemMapper;


	
	//(代理商后台)
	@Override
	public StatisAgentDay queryAddMemberSumAgent(AgentIndexInDTO agentIndexInDTO) throws Exception {
		return statisAgentDayMapper.queryAddMemberSumAgent(agentIndexInDTO);
	}

	@Override
	public StatisAgent queryMemberCount(AgentIndexInDTO agentIndexInDTO) throws Exception {
		return statisAgentMapper.queryMemberCount(agentIndexInDTO);
	}

	
	/*@Override
	public ProfitAgent queryProfitAmount(AgentIndexInDTO agentIndexInDTO) {
		agentIndexInDTO.setJoinStart(new Date());
		return profitAgentMapper.queryProfitAmount(agentIndexInDTO);
	}*/
	

	@Override
	public TradeOffline queryTotalAmount(AgentIndexInDTO agentIndexInDTO) {
		agentIndexInDTO.setJoinStart(new Date());
		return tradeOfflineMapper.queryTotalAmount(agentIndexInDTO);
	}
	
	

	@Override
	public int queryDeviceCount(AgentIndexInDTO agentIndexInDTO)throws Exception {
		return devicePosMapper.queryDeviceCount(agentIndexInDTO);
	}

	@Override
	public int queryDeviceSaleCount(AgentIndexInDTO agentIndexInDTO) throws Exception{
		agentIndexInDTO.setPosStatus(BizEnum.PosStatus.Fault.getType());
		return devicePosMapper.queryDeviceSaleCount(agentIndexInDTO);
	}

//	@Override
//	public List<AgentStatisAgentDayOutDTO> queryDayAmount(AgentIndexInDTO agentIndexInDTO) throws Exception{
//		return statisAgentDayMapper.queryDayAmount(agentIndexInDTO);
//	}
//

	@Override
	public Profit queryAgentProfitAmount(AgentIndexInDTO agentIndexInDTO) {
		agentIndexInDTO.setJoinStart(new Date());
		return profitMapper.queryAgentProfitAmount(agentIndexInDTO);
	}



	@Override
	public List<AgentIndexOutDTO> queryShopAmountMonth(AgentIndexInDTO agentIndexInDTO) throws Exception{
		agentIndexInDTO.setJoinStart(new Date());
		return statisShopMapper.queryShopAmountMonth(agentIndexInDTO);
	}
	@Override
	public List<MallGoodOutDTO> queryGoodCount(AgentIndexInDTO agentIndexInDTO) throws Exception {
		agentIndexInDTO.setJoinStart(new Date());
		return mallGoodMapper.queryGoodCount(agentIndexInDTO);
	}
	
	
	@Override
	public List<AgentStatisAgentDaySevenDataOutDTO> queryAgentSevenDayData(AgentIndexInDTO agentIndexInDTO)
			throws Exception {
		Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-6);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime();  
        agentIndexInDTO.setStatisDate(date);//
		System.out.println(agentIndexInDTO.getStatisDate());
		return statisAgentDayMapper.queryAgentSevenDayData(agentIndexInDTO);
	}
	
	//(渠道后台)首页
	/**
	 * 查询昨日新增会员数
	 */
//	@Override
//	public StatisGroupDay queryAddMemberSumGroup(GroupIndexInDTO groupIndexInDTO) throws Exception{
//
//		Date date = new Date();
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(date);
//        calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动
//        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
//        groupIndexInDTO.setStatisDate(date);//昨日
//
//		return statisGroupDayMapper.queryAddMemberSumGroup(groupIndexInDTO);
//	}

	@Override
	public Member queryAddMemberCount(GroupIndexInDTO groupIndexInDTO) {
		groupIndexInDTO.setStatisDate(new Date());
		return memberMapper.queryAddMemberCount(groupIndexInDTO);
	}

	@Override
	public Agent addAgentCount(GroupIndexInDTO groupIndexInDTO) {
		groupIndexInDTO.setStatisDate(new Date());
		return agentMapper.addAgentCount(groupIndexInDTO);
	}

	@Override
	public Shop addShopCount(GroupIndexInDTO groupIndexInDTO) {
		groupIndexInDTO.setStatisDate(new Date());
		return shopMapper.addShopCount(groupIndexInDTO);
	}

	@Override
	public List<Shop> queryAllShopList(Long groupId) throws Exception {
		return shopMapper.listShopByGroupId(groupId);
	}

	@Override
	public TradeOffline queryShopTrade(Long id) {
		return tradeOfflineMapper.queryShopTrade(id);
	}

	@Override
	public Profit queryShopThisMonthProfit(Long id) {
		return profitMapper.queryShopThisMonthProfit(id);
	}

	@Override
	public List<MemberOutDTO> queryWeekAddMemberCount(GroupIndexInDTO groupIndexInDTO) {
		return memberMapper.queryWeekAddMemberCount(groupIndexInDTO.getGroupId());
	}

	@Override
	public List<OrderServiceGoodDetail> queryWeekOrderCount(GroupIndexInDTO groupIndexInDTO) {
		return orderServiceGoodDetailMapper.queryOrderServiceData(groupIndexInDTO.getGroupId());
	}

	@Override
	public List<OrderGoodItem> queryWeekOrderGoodCount(GroupIndexInDTO groupIndexInDTO) {
		return orderGoodItemMapper.queryWeekOrderGoodCount(groupIndexInDTO.getGroupId());
	}

	@Override
	public TradeOffline queryGroupTotalTradeAmount(GroupIndexInDTO groupIndexInDTO) {
		return tradeOfflineMapper.queryGroupTotalTradeAmount(groupIndexInDTO.getGroupId());
	}

	@Override
	public Profit queryGroupProfitAmount(GroupIndexInDTO groupIndexInDTO) {
		return profitMapper.queryGroupProfitAmount(groupIndexInDTO.getGroupId());
	}

	@Override
	public OrderServiceGoodDetail queryMonthOrderServiceAmount(ShopIndexInDTO shopIndexInDTO) {
		return orderServiceGoodDetailMapper.queryMonthOrderData(shopIndexInDTO);
	}

	@Override
	public TradeOffline queryMonthVerifyCouponCount(ShopIndexInDTO shopIndexInDTO) {
		return tradeOfflineMapper.queryMonthVerifyCouponCount(shopIndexInDTO.getShopId());
	}

	@Override
	public CouponUser querySendCouponCount(ShopIndexInDTO shopIndexInDTO) {
		return couponUserMapper.querySendCouponCount(shopIndexInDTO.getShopId());
	}

	@Override
	public OrderServiceGoodDetail queryMonthOrderServiceGoodCount(ShopIndexInDTO shopIndexInDTO) {
		return orderServiceGoodDetailMapper.queryMonthOrderServiceGoodCount(shopIndexInDTO.getShopId());
	}

	@Override
	public OrderGoodItem queryOrderGood(ShopIndexInDTO shopIndexInDTO) {
		return orderGoodItemMapper.queryOrderGoodMonthData(shopIndexInDTO.getShopId());
	}

	@Override
	public List<OrderGoodItem> monthOrderGoodData(ShopIndexInDTO shopIndexInDTO) {
		return orderGoodItemMapper.queryMonthOrderGoodData(shopIndexInDTO.getShopId());
	}


	@Override
	public StatisGroup queryMemberCountGroup(GroupIndexInDTO groupIndexInDTO) throws Exception {
		return statisGroupMapper.queryMemberCountGroup(groupIndexInDTO);
	}

	@Override
	public DevicePos queryDeviceSum(GroupIndexInDTO groupIndexInDTO) {
		return devicePosMapper.queryDeviceSum(groupIndexInDTO);
	}

	
	@Override
	public int queryDeviceSaleSum(GroupIndexInDTO groupIndexInDTO) throws Exception{
		groupIndexInDTO.setPosStatus(BizEnum.PosStatus.Fault.getType());
		return devicePosMapper.queryDeviceSaleSum(groupIndexInDTO);
	}
	
	

	@Override
	public TradeOffline queryDayAmountGroup(GroupIndexInDTO groupIndexInDTO) throws Exception {
		groupIndexInDTO.setTodayStatisDate(new Date());
		return tradeOfflineMapper.queryDayAmount(groupIndexInDTO);
	}

	
	/*@Override
	public ProfitAgent queryGroupPorfitAmount(GroupIndexInDTO groupIndexInDTO) {
		groupIndexInDTO.setTodayStatisDate(new Date());
		return profitAgentMapper.queryGroupPorfitAmount(groupIndexInDTO);
	}*/
	
	@Override
	public List<MallGoodOutDTO> queryGoodCountGroup(GroupIndexInDTO groupIndexInDTO) throws Exception {
		groupIndexInDTO.setTodayStatisDate(new Date());
		return mallGoodMapper.queryGoodCountGroup(groupIndexInDTO);
	}

	@Override
	public List<GroupActivityBaseOutDTO> queryActivity(GroupIndexInDTO groupIndexInDTO) {
//		groupIndexInDTO.setTodayStatisDate(new Date());
		groupIndexInDTO.setStatus(ActivityEnum.ActivityStatus.Proccessing.getType());
		return activityBaseMapper.queryActivity(groupIndexInDTO);
	}

	@Override
	public List<StatisGroupDayOutDTO> querySevenDayData(GroupIndexInDTO groupIndexInDTO) {
//		Date date = new Date();
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(date);
//        calendar.add(calendar.DATE,-6);//把日期往后增加一天.整数往后推,负数往前移动
//        date=calendar.getTime();  
//        groupIndexInDTO.setStatisDate(date);
		
		return statisGroupDayMapper.querySevenDayData(groupIndexInDTO);
	}

	@Override
	public TradeOffline queryCardData(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTodayStatisDate(new Date());
		shopIndexInDTO.setTransactionType(BizEnum.TransactionType.Consumption.getType());	
		shopIndexInDTO.setSubType(BizEnum.PaymentScene.POS.getType());	
		return tradeOfflineMapper.queryCardData(shopIndexInDTO);
	}

	@Override
	public TradeOffline queryScanData(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTransactionType(BizEnum.TransactionType.Consumption.getType());
		return tradeOfflineMapper.queryScanData(shopIndexInDTO);
	}

	@Override
	public TradeOffline queryWalletData(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTodayStatisDate(new Date());
		shopIndexInDTO.setTransactionType(BizEnum.TransactionType.Consumption.getType());
		shopIndexInDTO.setPayType(BizEnum.PaymentType.Wallet.getType());
		return tradeOfflineMapper.queryWalletData(shopIndexInDTO);
	}

	@Override
	public TradeOffline queryDeskCardData(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTodayStatisDate(new Date());
		shopIndexInDTO.setTransactionType(BizEnum.TransactionType.Consumption.getType());	
		shopIndexInDTO.setSubType(BizEnum.PaymentScene.DeskCard.getType());
//		shopIndexInDTO.setSubType2(BizEnum.ConsumptionSubType.DeskCardAliPay.getType());
		return tradeOfflineMapper.queryDeskCardData(shopIndexInDTO);
	}

	@Override
	public OrderServiceGoodDetail queryOrderData(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTodayStatisDate(new Date());
		
		return orderServiceGoodDetailMapper.queryOrderData(shopIndexInDTO);
	}

	@Override
	public int queryMemberSum(ShopIndexInDTO shopIndexInDTO) throws Exception {

		return tradeOfflineMapper.queryMemberSum(shopIndexInDTO);
	}


	@Override
	public int queryDevMemberSum(ShopIndexInDTO shopIndexInDTO) throws Exception {
		return memberMapper.queryDevMemberSum(shopIndexInDTO);
	}
	


	@Override
	public int querySum(ShopIndexInDTO shopIndexInDTO) throws Exception {
		return orderServiceGoodDetailMapper.querySum(shopIndexInDTO);
	}
	




	
	
	@Override
	public StatisShop queryStarSum(ShopIndexInDTO shopIndexInDTO) throws Exception {
		
//		shopIndexInDTO.setTodayStatisDate(new Date());
		return statisShopMapper.queryStarSum(shopIndexInDTO);
	}

	@Override
	public Shop queryShopData(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTodayStatisDate(new Date());
		
		return shopMapper.queryShopData(shopIndexInDTO);
	}

	@Override
	public List<GroupActivityBaseOutDTO> queryShopActivity(ShopIndexInDTO shopIndexInDTO) throws Exception {
		shopIndexInDTO.setTodayStatisDate(new Date());
		return activityBaseMapper.queryShopActivity(shopIndexInDTO);
	}

	@Override
	public List<MemberCountOfflineOutDTO> queryMonthMemberConsumeCount(ShopIndexInDTO shopIndexInDTO) {
		return tradeOfflineMapper.queryMonthMemberConsumeCount(shopIndexInDTO);
	}

	@Override
	public List<Member> queryDevMemberCount(ShopIndexInDTO shopIndexInDTO) {
		return memberMapper.queryDevMemberCount(shopIndexInDTO);
	}

	

	
	@Override
	public List<StatisShopDay> queryMemberMonthData(ShopIndexInDTO shopIndexInDTO) {
		return statisShopDayMapper.queryMemberMonthData(shopIndexInDTO);
	}

	@Override
	public List<OrderServiceGoodDetail> queryOrderServiceGoodDetailData(ShopIndexInDTO shopIndexInDTO) {
		return orderServiceGoodDetailMapper.queryOrderServiceGoodDetailData(shopIndexInDTO);
	}

	@Override
	public List<CouponUser> queryCouponUserData(ShopIndexInDTO shopIndexInDTO) {
		
		return couponUserMapper.queryCouponUserData(shopIndexInDTO);
	}

	@Override
	public List<OrderServiceGood> queryMonthOrderNum(ShopIndexInDTO shopIndexInDTO) {
		return orderServiceGoodMapper.queryMonthOrderNum(shopIndexInDTO);
	}

	@Override
	public List<TradeOffline> queryMonthOrderAmount(ShopIndexInDTO shopIndexInDTO) {
		return tradeOfflineMapper.queryMonthOrderAmount(shopIndexInDTO);
	}


	@Override
	public TradeOffline queryMoney(ShopIndexInDTO shopIndexInDTO) {
		shopIndexInDTO.setTodayStatisDate(new Date());
		shopIndexInDTO.setPayType(BizEnum.PaymentType.Cash.getType());
		return tradeOfflineMapper.queryMoney(shopIndexInDTO);
	}

	@Override
	public Profit queryProfitAmount(GroupIndexInDTO groupIndexInDTO) throws Exception {
		groupIndexInDTO.setTodayStatisDate(new Date());
		return profitMapper.queryProfitAmount(groupIndexInDTO);
	}



}
